package com.zhx.basis.设计模式.责任链.demo2;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

public class VerificationServiceImpl implements IVerificationService {
//public class VerificationServiceImpl implements IVerificationService, InitializingBean {

    private IVerificationHandler verificationHandler;

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public Boolean isLegalMember(Member member, Activity activity) {
        //封装请求对象
        Request request = new Request();
        request.setMember(member);
        request.setActivity(activity);
        //发送请求
        Boolean result = verificationHandler.handleRequest(request).getIsOk();
        return result;
    }

    /**
     * 将请求处理者串联起来，创建请求处理者链
     *
     * @throws Exception
     */
    public  VerificationServiceImpl()  {
        IsPlusHandler isPlusHandler =new IsPlusHandler();
        HasFollowedShopHandler hasFollowedShopHandler = new HasFollowedShopHandler();
        HasBoughtHandler hasBoughtHandler =new HasBoughtHandler();
        isPlusHandler.setSuccessor(hasFollowedShopHandler);
        hasFollowedShopHandler.setSuccessor(hasBoughtHandler);
        this.verificationHandler = isPlusHandler;
    }
//    /**
//     * 将请求处理者串联起来，创建请求处理者链
//     *
//     * @throws Exception
//     */
//    @Override
//    public  VerificationServiceImpl()  {
//        IsPlusHandler isPlusHandler = (IsPlusHandler) applicationContext.getBean("isPlusHandler");
//        HasFollowedShopHandler hasFollowedShopHandler = (HasFollowedShopHandler) applicationContext.getBean("hasFollowedShopHandler");
//        HasBoughtHandler hasBoughtHandler = (HasBoughtHandler) applicationContext.getBean("hasBoughtHandler");
//        isPlusHandler.setSuccessor(hasFollowedShopHandler);
//        hasFollowedShopHandler.setSuccessor(hasBoughtHandler);
//        this.verificationHandler = isPlusHandler;
//    }
}
